#include "Common.hpp"

int main()
{
    FIFO f;  //  创建有名管道

    std::cout << "Server is running..." << std::endl;
    // 打开管道
    int fd = open(FIFO_FILE, O_RDONLY);
    if (fd == -1)
    {
        perror("open error");
        exit(FIFO_OPEN_ERROR);
    }
    // 开始通信
    while (true)
    {
        char buffer[MAX_BUFFER_SIZE] = {0};
        int n = read(fd, buffer, MAX_BUFFER_SIZE);
        if (n < 0)
        {
            perror("read error");
            exit(FIFO_READ_ERROR);
        }
        else if (n == 0)
        {
            std::cout << "Client 退出了,Server 也退出了" << std::endl;
            break;
        }
        else if (n > 0)
        {
            std::cout << "收到来自Client的消息:" << buffer << std::endl;
        }
    }

    // 关闭管道
    int c = close(fd);
    if (c == -1)
    {
        perror("close error");
        exit(FIFO_CLOSE_ERROR);
    }


    return 0;
}
